
#include <time.h>

#include <cv.h>
#include <highgui.h>

#include <stdio.h>

#include "Core.h"
#include "ShadowDB.h"

#include <vector>
#include <string>
using namespace std;

int main(int argc, char*argv[])
{
	if(argc != 3) {
		printf("usage: img db\n");
		exit(1);
	}
	ShadowDB db;
	
	time_t start_tm, finish_tm;	
	time(&start_tm);
	
	db.load(argv[2]);
	
	time(&finish_tm);
	cout << "load DB time " << difftime(finish_tm,start_tm) << endl;
	
	IplImage* pImg = NULL; 
	if((pImg = cvLoadImage(argv[1],CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
		printf("Can't load %s\n",argv[1]);
		exit(1);
	}

	int i,j;
	unsigned char *data;
	int height,width;
	height    = pImg->height;
	width     = pImg->width;
	data      = (unsigned char *)pImg->imageData;
	for(i=0;i<height;i++) for(j=0;j<width;j++) data[i*width+j] /= 255;

	time(&start_tm);
	const vector<Record> & cand = db.query(height,width,data);
	time(&finish_tm);
	cout << "query time " << difftime(finish_tm,start_tm) << endl;
	
	for(i=0;i<cand.size();i++)
		cout << cand[i] << endl;
	
	cvReleaseImage(&pImg);
	return 0;
}

